/**
  ******************************************************************************
  * @file           : linear_module.h
  * @author         : Mark Xiang
  * @brief          : brief
  ******************************************************************************
  * @attention
  *         1. GetWorkSpace方法需要外部更新位置信息(real_position)才能使用
  ******************************************************************************
  */
#ifndef __LINEAR_MODULE_H
#define __LINEAR_MODULE_H

/* ------------------------------ Includes ------------------------------ */

#include "stdint.h"
#include "pid.h"
#include "main.h"

/* ------------------------------ Defines ------------------------------ */

/* ------------------------------ Enum Typedef ------------------------------ */

/* ------------------------------ Struct Typedef ------------------------------ */

typedef struct
{
  /* 记录stepper真实位置，单位：脉冲数 */
  uint32_t real_count;
  /* 记录线性模组真实位置，单位：mm */
  float real_position;
  /* 记录stepper当前转动方向 */
  uint8_t directon;
  /* 记录限位触发情况 */
  uint8_t limit_extend;

  /* 用作速度控制的pid结构体 */
  Pid_t vel_pid_controller;

  /* 控制GPIO */
  GPIO_TypeDef *nENBL_PORT;
  uint16_t nENBL_Pin;
  GPIO_TypeDef *MODE0_PORT;
  uint16_t MODE0_Pin;
  GPIO_TypeDef *MODE1_PORT;
  uint16_t MODE1_Pin;
  GPIO_TypeDef *MODE2_PORT;
  uint16_t MODE2_Pin;
  GPIO_TypeDef *MODE3_PORT;
  uint16_t MODE3_Pin;
  GPIO_TypeDef *DIR_PORT;
  uint16_t DIR_Pin;

  /* 错误提示GPIO */
  GPIO_TypeDef *nFAULT_PORT;
  uint16_t nFAULT_Pin;

  /* 限位开关GPIO */
  GPIO_TypeDef *MIN_LIM_PORT;
  uint16_t MIN_LIM_Pin;
  GPIO_TypeDef *MAX_LIM_PORT;
  uint16_t MAX_LIM_Pin;

  /* PWM输出定时器通道 */
  TIM_HandleTypeDef *p_htim;
  uint32_t channel;
}LinearModule_t;

typedef struct
{
  /* pid结构体初始化 */
  float pid_controller_kp;
  float pid_controller_ki;
  float pid_controller_kd;

  /* 控制GPIO */
  GPIO_TypeDef *nENBL_PORT;
  uint16_t nENBL_Pin;
  GPIO_TypeDef *MODE0_PORT;
  uint16_t MODE0_Pin;
  GPIO_TypeDef *MODE1_PORT;
  uint16_t MODE1_Pin;
  GPIO_TypeDef *MODE2_PORT;
  uint16_t MODE2_Pin;
  GPIO_TypeDef *MODE3_PORT;
  uint16_t MODE3_Pin;
  GPIO_TypeDef *DIR_PORT;
  uint16_t DIR_Pin;

  /* 错误提示GPIO */
  GPIO_TypeDef *nFAULT_PORT;
  uint16_t nFAULT_Pin;

  /* 限位开关GPIO */
  GPIO_TypeDef *MIN_LIM_PORT;
  uint16_t MIN_LIM_Pin;
  GPIO_TypeDef *MAX_LIM_PORT;
  uint16_t MAX_LIM_Pin;

  /* PWM输出定时器通道 */
  TIM_HandleTypeDef *p_htim;
  uint32_t channel;
}LinearModuleInit_t;


/* ------------------------------ Variable Declarations ------------------------------ */

/* ------------------------------ Manager Typedef ------------------------------ */

typedef struct
{
  void (*Init)(LinearModule_t *module, LinearModuleInit_t init_struct);
  void (*SetVel)(LinearModule_t *module, float vel);
  void (*Stop)(LinearModule_t *module);
  void (*ResetPos)(LinearModule_t *module);
  float (*GetWorkSpace)(LinearModule_t *module);
}LinearModuleManager_typedef;


/* ------------------------------ Manager Extern ------------------------------ */

#endif

